<?php
namespace app\madmin\controller;
use app\madmin\model\MallKindsModel;
use app\madmin\model\MallTypesModel;

/**
 * 商品参数控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class MallKinds extends PublicBase{

    # 商品参数列表方法
    public function showList(){
        $MallKinds = new MallKindsModel;

        $Where = $this->getFormWhere();

        # 商品类型
        $kinds_types = input('param.kinds_types');
        if(!empty($kinds_types)){
            $Where['kinds_types'] = $kinds_types;
        }
        $this->assign('kinds_types', $kinds_types);

        # 参数类型
        $kinds_type = input('param.kinds_type');
        if(!empty($kinds_type)){
            $Where['kinds_type'] = $kinds_type;
        }
        $this->assign('kinds_type', $kinds_type);

        # 入录方式
        $kinds_input = input('param.kinds_input');
        if(!empty($kinds_input)){
            $Where['kinds_input'] = $kinds_input;
        }
        $this->assign('kinds_input', $kinds_input);

        # 参数状态
        $kinds_state = input('param.kinds_states');
        if(!empty($kinds_state)){
            $Where['kinds_state'] = $kinds_state;
        }
        $this->assign('kinds_state', $kinds_state);

        $Lists = $MallKinds->mList($Where, '*,types_name', config('CNF_LISTPAGE'));

        # 模版赋值
        return $this->fetch('MallKinds/showList', [
            'Lists' => $Lists,
            'Types' => $this->showTypes()
        ]);
    }

    # 商品参数新增方法
    public function showAdd(){
        $MallKinds = new MallKindsModel;

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallKinds->mAdd($Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            # 模版赋值
            return $this->fetch('MallKinds/showAdd', [
                'Info'  => Null,
                'Types' => $this->showTypes()
            ]);
        }
    }

    # 商品参数更新方法
    public function showEdit(){
        $MallKinds = new MallKindsModel;

        $ID = input('param.id');

        if(request()->isPost()){
            $Param = input('post.');

            $Back = $MallKinds->mEdit(['kinds_id'=>$ID], $Param);

            $this->ShowData($Back['code'], $Back['msgs'], $Back['urls']);
        }else{
            $Info = $MallKinds->mInfo(['kinds_id'=>$ID]);

            if(empty($Info)){
                $this->ShowData(2, lang('XtCwCxCw'));
            }else{
                # 模版赋值
                return $this->fetch('MallKinds/showAdd', [
                    'Info'  => $Info,
                    'Types' => $this->showTypes()
                ]);
            }
        }
    }

    # 商品参数删除方法
    public function showDel(){
        if(request()->isPost()){
            $MallKinds = new MallKindsModel;

            $ID = input('post.id');

            $Back = $MallKinds->mDel(['kinds_id'=>$ID]);

            $this->ShowData($Back['code'], $Back['msgs']);
        }
    }

    # 商品类型下拉方法
    private function showTypes(){
        $MallTypes = new MallTypesModel;

        $Where = [];
        $Where['types_state'] = 1;

        return $MallTypes->mAll($Where);
    }

}